package com.anolesoft.amljmes.fortyone.service.impl;

import java.util.List;

import org.apache.poi.ddf.EscherColorRef.SysIndexSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.anolesoft.amljmes.fortyone.dto.MesOrderTraceDto;
import com.anolesoft.amljmes.fortyone.dto.MesStoveFabinfoDto;
import com.anolesoft.amljmes.fortyone.dto.StoveDispatchQueryParamDto;
import com.anolesoft.amljmes.fortyone.service.MesStoveFabinfoService;
import com.anolesoft.amljmes.fortyone.service.mapper.MesOrderTraceMapper;
import com.anolesoft.amljmes.fortyone.service.mapper.MesStoveFabinfoServiceMapper;
import com.anolesoft.amljmes.fortyone.service.mapper.StfEmployeeMapper;
import com.anolesoft.amljmes.fortyone.vo.MesOrderTraceBean;
import com.anolesoft.amljmes.fortyone.vo.MesStoveFabinfoVo;
import com.anolesoft.amljmes.fortyone.vo.StoveInpectJsonBean;
import com.anolesoft.amljmes.test.fenguangyingdu.dto.FenguangYingduEmpDTO;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.security.util.SecurityUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itextpdf.text.log.SysoCounter;

@Service
public class MesStoveFabinfoServiceImpl implements MesStoveFabinfoService {
	@Autowired
	MesStoveFabinfoServiceMapper mesStoveFabinfoServiceMapper;
	@Autowired
	StfEmployeeMapper stfEmployeeMapper;
	@Autowired
	MesOrderTraceMapper mesOrderTraceMapper;
	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoAll(String stoveFabinfoObj) {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoAll(stoveFabinfoObj);
	}

	@Override
	public int updateMesStoveFabinfoDelete(List<String> objuids) {
		int count = 0;
		for (String objuid : objuids) {
			count += mesStoveFabinfoServiceMapper.updateMesStoveFabinfoDelete(objuid);
		}
		return count;
	}

	@Override
	public int updateMesStoveFabinfoCancel(List<String> objuids) {
		int count = 0;
		for (String objuid : objuids) {
			count += mesStoveFabinfoServiceMapper.updateMesStoveFabinfoCancel(objuid);
		}
		return count;
	}

	/**
	 * 我觉得写的没啥问题拿来用了
	 */
	@Transactional
	@Override
	public int updateMesStoveFabinfoSl(StoveInpectJsonBean stoveInpectJsonBean) {
		String userObjuid = stfEmployeeMapper.queryFactoryIdPassword2(stoveInpectJsonBean.getPassword2());
//		System.out.println("我是检验人_____________________"+userObjuid);
		if(userObjuid == null) {
			return 2;
		}
		stoveInpectJsonBean.getData().forEach(t -> {
			t.setInpectUser(userObjuid);
			mesStoveFabinfoServiceMapper.updateMesStoveFabinfoSl(t);
//			应该用扫条码的人吧，为什么要拿当前登录人呢
//			String lastchecker = SecurityUtils.getLoginUser().getEmpUid();
			int checknum = t.getYssl();
			int goognum = t.getHgps();
			int rangbunum = t.getRbps();
			int rejectnum = t.getFps();
			int onesoknum = t.getYcjyhgs();
			String notes = t.getRemark();
			String fabrecordid = t.getFortyoneCode();
//			有主键为什么要用这个
//			String processno = t.getFortyoneGxid();
//			String processname = t.getFortyoneGxname();
			MesOrderTraceBean mesorder = mesOrderTraceMapper.queryxiayigefongxu(fabrecordid);
			mesOrderTraceMapper.updateCurgongxu(mesorder);
			
			mesStoveFabinfoServiceMapper.updateFabinfoJy(userObjuid, checknum, goognum, rangbunum, rejectnum,
					onesoknum, notes, fabrecordid, t.getMesProcessUid());
			mesStoveFabinfoServiceMapper.updateJyys(t.getMesProcessUid());
			if (t.getChecklist3() != null) {
				mesStoveFabinfoServiceMapper.insertJianbiao("j3", t.getChecklist3(), t.getMesProcessUid(),
						t.getFortyoneCode(), userObjuid);
			}
			if (t.getChecklist5() != null) {
				mesStoveFabinfoServiceMapper.insertJianbiao("j5", t.getChecklist5(), t.getMesProcessUid(),
						t.getFortyoneCode(), userObjuid);
			}
			if (t.getChecklist6() != null) {
				mesStoveFabinfoServiceMapper.insertJianbiao("j6", t.getChecklist6(), t.getMesProcessUid(),
						t.getFortyoneCode(), userObjuid);
			}
			if (t.getChecklist62() != null) {
				mesStoveFabinfoServiceMapper.insertJianbiao("j62", t.getChecklist62(), t.getMesProcessUid(),
						t.getFortyoneCode(), userObjuid);
			}
		});

		return 1;
	}

	// 根据炉次id查工序名
	@Override
	public MesStoveFabinfoDto MesStoveFabinfoByGxname(String StoveFabinfoObj) {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByGxname(StoveFabinfoObj);
	}

	// 查胸卡号密码
	@Override
	public FenguangYingduEmpDTO queryEmpPassword(String objuid) {
		return mesStoveFabinfoServiceMapper.queryEmpPassword(objuid);
	}

	// 查Password2
	@Override
	public FenguangYingduEmpDTO queryEmpPassword2(String objuid) {
		return mesStoveFabinfoServiceMapper.queryEmpPassword2(objuid);
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByRc() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByRc();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByQx() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByQx();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByGz() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByGz();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByCx() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByCx();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByYdjc() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByYdjc();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByJz() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByJz();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByJy() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByJy();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByTd() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByTd();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByD() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByD();
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoByHor() {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByHor();
	}

	@Override
	public int addMesStoveFabinfo(MesStoveFabinfoDto inFoDtoParam) {
		int count = mesStoveFabinfoServiceMapper.addMesStoveFabinfo(inFoDtoParam);
		return count;
	}

	@Transactional
	@Override
	public int insertMesStoveFabinfo(List<MesStoveFabinfoDto> amStoveFabinfoDto) {
		int count = 0;
		for (MesStoveFabinfoDto s : amStoveFabinfoDto) {
			int i = mesStoveFabinfoServiceMapper.selectBctm(s);
			System.out.println(">>>>>>>" + i);
			if (i == 0) {
				count += mesStoveFabinfoServiceMapper.insertMesStoveFabinfo(s);
				System.out.println("^^^^^^^^^^^^^" + s);
			} else {
				continue;
			}
		}
		return count;
	}

	// 41厂工序名是否包含工序名关键字符
	@Override
	public int liketMesStoveFabinfo(String objuid, String curgongxuname) {
//  return this.mesStoveFabinfoServiceMapper.liketMesStoveFabinfo(objuid,curgongxuname);
		int count = mesStoveFabinfoServiceMapper.liketMesStoveFabinfo(objuid, curgongxuname);
		return count;
	}

	// 根据炉次编号查询所在炉次内的制造记录有哪些
	@Override
	public List<MesOrderTraceDto> mesStoveFabinfoZzjl(String stoveFabinfoObj) {
		return mesStoveFabinfoServiceMapper.mesStoveFabinfoZzjl(stoveFabinfoObj);
	}

	@Override
	public MesOrderTraceDto MesStoveFabinfoByZzjl(String fabrecordid) {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoByZzjl(fabrecordid);
	}

	@Override
	public List<MesStoveFabinfoDto> MesStoveFabinfoBy41(String StoveFabinfoObj) {
		return mesStoveFabinfoServiceMapper.MesStoveFabinfoBy41(StoveFabinfoObj);
	}

	/**
	 *
	 * @param lcid     炉次id
	 * @param workcode 工人胸卡号
	 * @param worker   工人姓名
	 */
	@Override
	@Transactional
	public void updateFabinfo(String lcid, String workcode, String worker) {
		/*
		 * 通过 炉次id 工人胸卡号 工人姓名 到mes_stove_fabinfo表中查询制造记录对象MesStoveFabinfoDto集合
		 */
		List<MesStoveFabinfoDto> mesStoveFabinfoDtos = mesStoveFabinfoServiceMapper.MesStoveFabinfoBy41(lcid);
		// fortyone_code,fortyone_gxid,fortyone_gxmane
		/*
		 * 遍历这个集合 修改反馈信息
		 */
		mesStoveFabinfoDtos.forEach(t -> {
			/*
			 * 修改mes_process_trace 产品制造记录表的反馈信息
			 */
			mesStoveFabinfoServiceMapper.updateFabinfo(t.getFortyoneCode(), workcode, worker, t.getFortyoneGxid());
			/*
			 * 修改制造记录 mes_process_sorren 颜色表
			 */
			mesStoveFabinfoServiceMapper.updateCzys(t.getMesProcessUid());
		});
	}

	// 根据炉次编号查询所在炉次内的标记有哪些
	@Override
	public List<MesStoveFabinfoDto> mesStoveFabinfobiaoji(String stoveFabinfoObj) {
		return mesStoveFabinfoServiceMapper.mesStoveFabinfobiaoji(stoveFabinfoObj);
	}

	@Override
	public Page<MesStoveFabinfoVo> getMesStoveFabinfoVOs(StoveDispatchQueryParamDto paramDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		paramDto.getCondition().setFactoryId(loginUser.getFacCode());
		Page<MesStoveFabinfoVo> page = new Page<>(paramDto.getPageNo(), paramDto.getPageSize());
		List<MesStoveFabinfoVo> stoveVos = mesStoveFabinfoServiceMapper
				.queryMesStoveFabinfoVOsByCondition(paramDto.getCondition(), page);
		page.setRecords(stoveVos);
		return page;
	}
	
	@Transactional
	@Override
	public void deleteDispatch(List<String> objuids) {
		objuids.forEach(objuid -> {
			mesStoveFabinfoServiceMapper.deleteByObjuid(objuid);
		});
	}
}
